<?php
require_once(dirname(__FILE__) . '/include/config.inc.php');


$dosql->Execute("SELECT * FROM  `fa_cms_archives`  WHERE `model_id`=3   ORDER BY id asc", 8);
while ($row3 = $dosql->GetArray(8)) {
    $aid = $row3['tbid'];
    $id = $row3['id'];
    $url = "http://www.mysd168.com/api/goods/goodinfo?goodid=" . $aid;

    $res = httpGet($url);
    if (preg_match('/^\xEF\xBB\xBF/', $res)) $res = substr($res, 3);

    $arr = json_decode($res, true);

    if ($arr) {
        $info = $arr['data']['info'];
        $content = GetContFile($info);
        $dosql->Execute("update  `fa_cms_addonproduct`  set  content='$content' where id='$id' ");
        echo $id."更新成功<br>";
    }

}

function httpGet($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    //curl_setopt($curl,CURLOPT_CAINFO,dirname(__FILE__).'/cacert.pem');//这是根据http://curl.haxx.se/ca/cacert.pem 下载的证书，添加这句话之后就运行正常了
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);

    curl_close($curl);

    return $res;
}


/*
 * 获取一个远程图片
 *
 * @access  public
 * @param   $url       string  获取字段所属模型
 * @return  $save_url  string  返回上传后地址
*/
function GetRemPic($url)
{

    $filename_arr = parse_url($url);
    $path = $filename_arr['path'];
    $filename_arr = explode('/', $path);
    $filename = array_pop($filename_arr);

    $upload_url = '/uploads/goodsDetail/';
    $upload_dir = PHPMYWIND_UPLOAD . "/goodsDetail/";

    //上传文件路径
    $save_url = $upload_url . $filename;
    $save_dir = $upload_dir . $filename;
    echo "保存路径" . $save_dir . "<br/>";

    if (file_exists($save_dir)) {
        return $save_url;
    }


    //引入下载类
    require_once(PHPMYWIND_DATA . '/httpfile/down.class.php');

    //初始化变量
    $htd = new HttpDown();
    $htd->OpenUrl($url);


    //判断文件类型
    $sparr = array('image/pjpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/xpng', 'image/wbmp');
    if (!in_array($htd->GetHead("content-type"), $sparr)) {
        return FALSE;
    } else {
        $type = $htd->GetHead("content-type");

        if ($type == 'image/gif')
            $tempfile_ext = 'gif';

        else if ($type == 'image/png')
            $tempfile_ext = 'png';

        else if ($type == 'image/wbmp')
            $tempfile_ext = 'bmp';

        else
            $tempfile_ext = 'jpg';


        echo "保存";

        $rs = $htd->SaveToBin($save_dir);
    }

    $htd->Close();
    return ($rs ? $save_url : '');
}



//获得文章内容里的外部资源
function GetContFile($body)
{

    global $cfg_image_dir;

    //引入下载类
    require_once(PHPMYWIND_DATA.'/httpfile/down.class.php');

    //初始化变量
    $body = stripslashes($body);
    $host = 'http://'.$_SERVER['HTTP_HOST'];

    //过滤图片文件
    $pic_arr = array();
    //preg_match_all("/src=[\"|'|\s]{0,}(http:\/\/([^>]*)\.(gif|jpg|png|bmp))/isU", $body, $pic_arr);


    preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/', $body, $pic_arr);


    $pic_arr = array_unique($pic_arr[1]);

    //初始化下载类
    $htd = new HttpDown();

    foreach($pic_arr as $k=>$v)
    {

        $v_true="http://www.mysd168.com".$v;


        $htd->OpenUrl($v_true);


        $type = $htd->GetHead('content-type');


        if($type == 'image/gif')
            $tempfile_ext = 'gif';

        else if($type == 'image/png')
            $tempfile_ext = 'png';

        else if($type == 'image/wbmp')
            $tempfile_ext = 'bmp';

        else
            $tempfile_ext = 'jpg';


        $upload_url = 'image';
        $upload_dir = $cfg_image_dir;

        $ymd = date('Ymd');
        $upload_url .= '/'.$ymd;
        $upload_dir .= '/'.$ymd;

        if(!file_exists($upload_dir))
        {
            mkdir($upload_dir);

            $fp = fopen($upload_dir.'/index.htm', 'w');
            fclose($fp);
        }

        //上传文件名称
        $filename = time()."_".rand(1,9999).'.'.$tempfile_ext;

        //上传文件路径
        $save_url = 'uploads/'.$upload_url.'/'.$filename;

        //生成本地路径
        $self = explode('/', $_SERVER['PHP_SELF']);
        $self_size = count($self) - 2;
        $self_str  = '';
        for($i=0; $i<$self_size; $i++)
        {
            $self_str .= $self[$i].'/';
        }

        $save_url = $self_str.'uploads/'.$upload_url.'/'.$filename;
        $save_dir = $upload_dir.'/'.$filename;

        $rs = $htd->SaveToBin($save_dir);
        var_dump($rs);
        if($rs)
        {
            $body = str_replace(trim($v), "/".$save_url, $body);
        }
    }

    $htd->Close();


    //回传转义字符串
    return $body;
}


?>